<template>
   <!-- 头部 -->
   <LoginHeader>联合登录</LoginHeader>
   <!-- 主体 -->
   <!-- 加载效果 -->
   <section class="container" v-if="isBind">
        <div class="unbind">
            <div class="loading"></div>
        </div>
   </section>
   <section class="container" v-else>
    <nav class="tab">
      <a @click="hasAccount=true" :class="{active:hasAccount}" href="javascript:;">
        <i class="iconfont icon-bind" />
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a @click="hasAccount=false" :class="{active:!hasAccount}" href="javascript:;">
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content" v-if="hasAccount">
      <CallbackBind :unionId="unionId" />
    </div>
    <div class="tab-content" v-else>
      <CallbackPatch :unionId="unionId"/>
    </div>
  </section>

   <!-- 底部 -->
   <LoginFooter></LoginFooter>

</template>
<script>
import LoginHeader from './components/login-header.vue'
import LoginFooter from './components/login-footer.vue'
import CallbackBind from './components/callback-bind.vue'
import CallbackPatch from './components/callback-patch.vue'
import Message from '@/components/library/xtx-message.js'
// import QC from 'qc'
import {userQQLogin} from '@/api/user'
import {ref} from 'vue'
import { useRouter } from 'vue-router'
import {useStore} from 'vuex'
export default {
    name: 'PageCallback',
    components: {LoginHeader, LoginFooter, CallbackBind, CallbackPatch},
    setup () {
        const unionId = ref(null)
        const hasAccount = ref(true)
        const isBind = ref(true)
        const store = useStore()
        const router = useRouter()
        //首先：默认已注册且已绑定
        //通过qq的api获取openId就是后台需要的unionId然后去登录
        //如果成功：登陆成功
        //如果失败：有账号未绑定qq,没有账号未绑定qq

        //确保QQ已登录
        if(QC.Login.check()) {

            //获取第三方唯一标识QQ唯一标识
            QC.Login.getMe((openId) => {
                unionId.value = openId
                //请求小兔仙后台，做QQ登录
            userQQLogin(openId).then(data => {
                //登陆成功、
                //存储用户信息
                const { id, account, avatar, mobile, nickname, token } = data.result
                store.commit('user/setUser', { id, account, avatar, mobile, nickname, token })
                //合并购物车
                store.dispatch('cart/mergeCart').then(() => {
                    //成功跳转页面
                  router.push(store.state.user.redirectUrl)
                  Message({type: 'success', text: 'QQ登录成功'})
                })
                

            }).catch (e => {
                //失败, 没绑定qq
                isBind.value = false

            })

            })

        }



        return {hasAccount,isBind, unionId}

    }

}
</script>

<style scoped lang='less'>
.container {
  padding: 25px 0;
  height: 730px;
  position: relative;
  .unbind {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    padding: 25px 0;
    z-index: 99;
    .loading {
      height: 100%;
      background: #fff url(../../assets/images/load.gif) no-repeat center / 100px 100px;
    }
  }
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>